package naga.x.platform2.channel.iapppay.support;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import naga.x.platform2.app.Channel;
import naga.x.platform2.channel.iapppay.support.config.ArkfelConfig;
import naga.x.platform2.channel.iapppay.support.config.IAppPayConfig;
import naga.x.platform2.channel.iapppay.support.config.TelecomConfig;
import naga.x.platform2.utils.CommonUtils;
import naga.x.platform2.utils.IOUtils;
import naga.x.platform2.utils.StringUtils;

@Component
public class IAppPayApi {

	public static final Logger LOG = LoggerFactory.getLogger(IAppPayApi.class);
	public static final String DEFAULT_CHARSET = "UTF-8";
	public static final String DEFAULT_SIGN_TYPE = "RSA";
	public static final String PARAMETER_KEY_DATA = "transdata";
	public static final String PARAMETER_KEY_SIGN = "sign";
	public static final String PARAMETER_KEY_SIGN_TYPE = "signtype";
	
	@Autowired
	private IAppPayConfig iappConfig;
	@Autowired
	private TelecomConfig telecomConfig;
	@Autowired
	private ArkfelConfig arkfelConfig;
	
	public byte[] makeRequestParameters(BaseIAppPayConfig config, Object data) {
		return makeRequestParameters(config, JSON.toJSONString(data));
	}
	
	public byte[] makeRequestParameters(BaseIAppPayConfig config, JSONObject data) {
		return makeRequestParameters(config, data.toJSONString());
	}
	
	public byte[] makeRequestParameters(BaseIAppPayConfig config, String data) {
		String params = makeRequestParametersAsText(config, data);
		return IOUtils.getStringBytes(params, DEFAULT_CHARSET);
	}
	
	public String makeRequestParametersAsText(BaseIAppPayConfig config, String data) {
		String transdata = CommonUtils.encodeURL(data, DEFAULT_CHARSET);
		String sign = CommonUtils.encodeURL(RSA.sign(data, config.getPrivateKey()), DEFAULT_CHARSET);
		String params = PARAMETER_KEY_DATA + "=" + transdata
				+ "&" + PARAMETER_KEY_SIGN + "=" + sign
				+ "&" + PARAMETER_KEY_SIGN_TYPE + "=" + DEFAULT_SIGN_TYPE;
		return params;
	}
	
	public static JSONObject makeError(String code, String msg) {
		JSONObject error = new JSONObject(2, true);
		error.put("code", code);
		error.put("errmsg", msg);
		return error;
	}
	
	public static Map<String, String> parseUrlParameters(String plainText) {
		String[] strs = StringUtils.split(plainText, "&");
		Map<String, String> map = new HashMap<String, String>(4);
		for (String str : strs) {
			int n = str.indexOf('=');
			String key = str.substring(0, n);
			String val = str.substring(n + 1);
			val = CommonUtils.decodeURL(val, DEFAULT_CHARSET); // try decode
			map.put(key, val);
		}
		return map;
	}
	
	public BaseIAppPayConfig getConfig(String channel) {
		if ("iapppay".equals(channel)) {
			return iappConfig;
		}
		if ("telecom".equals(channel)) {
			return telecomConfig;
		}
		if ("arkfel".equals(channel)) {
			return arkfelConfig;
		}
		return null;
	}
	
	public static Channel getChannel(String channel) {
		if ("iapppay".equals(channel)) {
			return Channel.IAPP_PAY;
		}
		if ("telecom".equals(channel)) {
			return Channel.TELECOM;
		}
		if ("arkfel".equals(channel)) {
			return Channel.ARKFEL;
		}
		return null;
	}
	
}

//appid  3008763868 
//应用私钥（APPV_KEY:PKCS1 适用平台：C++，PHP，python，Android，IOS）
//MIICXQIBAAKBgQC/WNbepbvuB57zBPQdiW++nfMrTKrWtsLne33gp2yoDnQBk7brkuig8Z0jpN5D2A4gwI2oS7UHZ78WY26Uvhc+E1UYhad2ujiKU0k2L/1dBeZy+tibJrKBGVvdsVnbp5qoDwjg0aEsG9J11az0Bl41Tilu86ORS885RSSTmQ+rTwIDAQABAoGBAKKnBPd8EHT0+3N+yXf2JQv2vgcXg71C3Ajjm4wFJcLxjkDs7LfxSFfPKpFrCxjGprPGmmPt/vNNiEyitxQvRhcg70TIifUz8jfezyPQKbhWaVz5DMIwZ37zYJgO9kDaDN5B+5w4JIm44TGS69H9VxuCHWZYDEUbWBSZeZ1uWu55AkEA48uQTtxJM2Xf8XcPh4hxeOPCcO7e7T7YNJsmdDV5JE60eaCz780JjUeKyMBH2DAOOzKLCBcUu4PtCIRYnR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